package com.franklin.java.api.state.converter;

import com.intellij.ui.JBColor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.awt.*;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Franklin
 * @since 2021/9/2 18:42
 */
public class ColorConverter extends AbstractConverter<Color> {

    private final static String SEPARATOR = ";";

    @Override
    protected Color getDefaultValue() {
        return JBColor.PINK;
    }

    @Override
    public @Nullable Color fromString(@NotNull String s) {
        String[] split = s.split(SEPARATOR);
        List<Integer> collect = Arrays.stream(split)
            .map(Integer::parseInt)
            .collect(Collectors.toList());
        return new Color(collect.get(0),collect.get(1),collect.get(2),collect.get(3));
    }

    @Override
    public @Nullable String toString(@NotNull Color color) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder
            .append(color.getRed()).append(SEPARATOR)
            .append(color.getGreen()).append(SEPARATOR)
            .append(color.getBlue()).append(SEPARATOR)
            .append(color.getAlpha());
        return stringBuilder.toString();
    }
}
